#ifndef __STDP_FORMAT_H
#define __STDP_FORMAT_H
#include <stdint.h>
#include "fiip-base/fiip.h"

#ifdef __cplusplus
extern "C" {
#endif

#define FIIP_PROTOCOL_TYPE_STDP 0x00
#define STDP_VERSION 0x03

typedef struct {
  uint8_t leading[3];   // 前导
  uint8_t startTag[2];  // 起始标识
  uint8_t srcAddr[8];   // 源地址
  uint8_t dstAddr[8];   // 目标地址
  uint8_t ttl[1];       // 生存时间
  uint8_t type[1];      // 协议类型

  uint8_t version[1];                // stdp版本号
  uint8_t control[1];                // 控制字段
  uint8_t retransmit[1];             // 重传
  uint8_t reserve1[2];               // 预留1
  uint8_t index[2];                  // 命令序列号
  uint8_t cmd[2];                    // 命令标识
  uint8_t len[2];                    // 消息体长度
  uint8_t body[FRAME_LEN_MAX - 41];  // 消息体

  uint8_t crc[2];       // CRC校验
  uint8_t endTag[2];    // 结束标识
  uint8_t trailing[3];  // 后继
} Stdp_FieldStruct;     // 数据结构
typedef enum {
  stdp_field_leading = 0,
  stdp_field_startTag = 3,
  stdp_field_srcAddr = 5,
  stdp_field_dstAddr = 13,
  stdp_field_ttl = 21,
  stdp_field_type = 22,
  stdp_field_version = 23 + 0,
  stdp_field_control = 23 + 1,
  stdp_field_retransmit = 23 + 2,
  stdp_field_reserve1 = 23 + 3,
  stdp_field_index = 23 + 5,
  stdp_field_cmd = 23 + 7,
  stdp_field_len = 23 + 9,
  stdp_field_body = 23 + 11,
  stdp_field_crc = FRAME_LEN_MAX - 7,
  stdp_field_endTag = FRAME_LEN_MAX - 5,
  stdp_field_trailing = FRAME_LEN_MAX - 3,
} Stdp_FieldEnum;

typedef struct {
  FormatStruct* frame;
  uint8_t* buf;
  uint16_t* bufLen;
  Stdp_FieldStruct* msg;
  uint8_t _index[2];  //序列号
} StdpStruct;         //数据结构

StdpStruct* stdp_new(void);
void stdp_del(StdpStruct* self);
void stdp_setVar(StdpStruct* self,
                 Stdp_FieldEnum field,
                 uint8_t* data,
                 uint16_t dataLen);
uint8_t* stdp_getData(StdpStruct* self);
Stdp_FieldStruct* stdp_getMsg(StdpStruct* self);
void stdp_respond(StdpStruct* self,
                  Stdp_FieldStruct* msg,
                  uint8_t error,
                  uint8_t* body,
                  uint8_t bodyLen);

#ifdef __cplusplus
}
#endif
#endif  //__STDP_FORMAT_H
